//
//  ViewController.m
//  UI29_Block
//
//  Created by dllo on 16/1/20.
//  Copyright © 2016年 lanou.com. All rights reserved.
//

#import "ViewController.h"
#import "SecendViewController.h"
#import "TouchView.h"

#import "NetworrkingHandler.h"


/* 函数的声明.*/
int sum(int, int);

/* 函数的实现.*/

int sum(int x, int y){

    return x + y;
}

int sub(int x, int y) {

    return x - y;
}


@interface ViewController ()

@property (nonatomic, copy) void(^block)(NSString *);
@property (weak, nonatomic) IBOutlet TouchView *touchView;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self functionReview];
    [self studyBlock];
    
    [self.touchView viewDidClick:^(TouchView *view) {
        NSLog(@"wwwwwww");
        
    }];
    
        [self.touchView addButtonHandle:^(UIButton *addButton) {
        NSLog(@"heheh");
    } deleteButtonHandle:^(UIButton *deleteButton) {
        NSLog(@"hahah");
    }];
    
    [self.touchView buttonHandle:^(UIButton *button) {
        
        NSLog(@"zhende 是我");
    }];
    

    [self handleData];
    
}

#pragma mark - 数据处理(网络类使用Block回调)
- (void)handleData {

    [NetworrkingHandler netWorkingWithURL:@"http://api.map.baidu.com/place/v2/search?query=银行&region=大连&output=json&ak=6E823f587c95f0148c19993539b99295" completeHandle:^(NSData * _Nullable data, NSURLResponse * _Nullable response,id result) {
        
        NSLog(@"%@, %@, %@",data,response,  result);
       
        
    }];
}




#pragma mark - 函数复习
- (void)functionReview {

    /** 
     * 函数由三部分:函数的声明,函数的实现,函数的调用.
     * 
     *
     **/

    NSLog(@"%d",sum(3, 5));
    
    /** 
     * 函数指针:指针指向一块内存,里面是一段代码段.
     *
     */

    /** 定义一个函数指针变量.*/
    int (*p)(int, int) = NULL;
    
    // 函数指针变量赋值.
    p = sum;
    p = sub;
    
    // 通过函数指针来调用函数.
    NSLog(@"%d",p(3,5));
}

#pragma mark - block
- (void)studyBlock {

    /* 
     * 1. block 和函数指针很相似. 技巧: *p 换成 ^block
     * 2. Block 相当于匿名函数.所以 Block变量赋值时,直接赋函数实现部分.
     */
    int (^block1)(int, int) = ^(int x, int y) {
        return x + y;
    };
    
    // 调用block.
    
    NSLog(@"%d", block1(3,5));
    
    //  block 语法.
    // 无参无返block.
    void(^block2)(void) = ^(void) {
        NSLog(@"lalalalala");
    };
    // 调用.
   block2();

    // 无参有返.
    NSString *(^block3)(void) = ^NSString *(void) {
    
        return @"无参有返";
    };
    block3();
    NSLog(@"%@",block3());
    
    // 有参无返.
    void (^block4)(NSString *, NSString *) = ^(NSString *str1, NSString *str2) {
        NSLog(@"%@",[str1 stringByAppendingString:str2]);
    };
    block4(@"da",@"lian");
    
    // 有参有返.
    NSString *(^block5)(NSString *,NSString *) = ^NSString *(NSString *str1,NSString *str2) {
     return [str1 stringByAppendingString:str2];
    };
    NSLog(@"%@",block5(@"hello",@"world"));
    
    self.block = ^(NSString *str) {
    
        NSLog(@"%@",str);
    
    };
    
}
- (IBAction)handleBlock:(id)sender {
    
    // 调用block
    self.block(@"aaaa");
    
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // 获取第二界面对象.

    SecendViewController *secend = [segue destinationViewController];
    
    


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
